package com.zrar.easyweb.demo.uc.controller;

import com.zrar.easyweb.demo.uc.dao.UserDao;
import com.zrar.easyweb.demo.uc.service.UserService;
import com.zrar.easyweb.demo.uc.vo.UserDetails;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 用户服务
 *
 * @author green
 * @date 2019/10/15
 * @company 安人股份
 * @desc
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户服务")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Resource
    private UserService userService;

    @ApiOperation(value = "用户详情查询", httpMethod = "GET", notes = "通过用户ID查询用户详情")
    @GetMapping(value = "/detail/{userId}", produces = {"application/json; charset=utf-8"})
    public UserDetails detail(@PathVariable("userId") String userId) {
        UserDetails details = userService.getUserById(userId);
        return details;
    }

    @ApiOperation(value = "用户列表", httpMethod = "GET", notes = "查询所有用户信息")
    @GetMapping(value = "/list", produces = {"application/json; charset=utf-8"})
    public List<UserDetails> list() {
        List<UserDetails> list = userService.getUserList();
        return list;
    }


    @ApiOperation(value = "邮箱地址更新", httpMethod = "PUT", notes = "更新用户邮箱地址")
    @PutMapping(value = "/email", produces = {"application/json; charset=utf-8"})
    public ResponseEntity updateEmail(@Valid @RequestParam("userId") String userId,
                                      @Valid @RequestParam("email") String email) {
        boolean flag = userService.updateUserEmail(userId, email);
        return ResponseEntity.ok().body(flag);
    }

}
